package com.atguigu.flink0922.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/12 8:45
 */
public class Flink08_SQL_Window_Group {
    
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("create table sensor(" +
                            "id string," +
                            "ts bigint," +
                            "vc int, " +
                            "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss'))," +
                            "watermark for t as t - interval '5' second)" +
                            "with("
                            + "'connector' = 'filesystem',"
                            + "'path' = 'input/sensor.txt',"
                            + "'format' = 'csv'"
                            + ")");
        
        /*tEnv
            .sqlQuery("select " +
                          " id, " +
                          " tumble_start(t, interval '5' second) w_start, " +
                          " tumble_end(t, interval '5' second) w_end" +
                          " from sensor " +
                          " group by id, tumble(t, interval '5' second)")
            .execute()
            .print();*/
    
        /*tEnv
            .sqlQuery("select " +
                          " id, " +
                          " hop_start(t, interval '2' second,interval '5' second) w_start, " +
                          " hop_end(t, interval '2' second,interval '5' second) w_end " +
                          "from sensor " +
                          "group by id, hop(t, interval '2' second,interval '5' second)")
            .execute()
            .print();*/
    
    
        tEnv
            .sqlQuery("select " +
                          " id, " +
                          " session_start(t, interval '5' second) w_start, " +
                          " session_end(t, interval '5' second) w_end," +
                          " sum(vc)" +
                          "from sensor " +
                          "group by id, session(t, interval '5' second)")
            .execute()
            .print();
    }
}
